package com.calvin.study.multithread.thread;

public class Thread04 {

	public static void main(String[] args) {
		SellTicket s01 = new SellTicket();

		new Thread(s01).start();
		new Thread(s01).start();
		new Thread(s01).start();

	}

}

//售票 extends Thread
class SellTicket implements Runnable {

	private int ticketNum = 100;

	private boolean flag = true;

	public synchronized void sell() {
		if (ticketNum <= 0) {
			System.out.println("票已售罄");
			flag = false;
			return;
		}

		try {
			Thread.sleep(50);
		} catch (Exception e) {
			// TODO: handle exception
		}
		System.out.println("窗口" + Thread.currentThread().getName() + "已售出一张票，" + "剩余票数：" + (--ticketNum));

	}

	@Override
	public void run() {
		while (flag) {
			sell();
		}
	}

}
